﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edgar.Entities.Characteristics;
using System.Xml.Serialization;
using System.IO;

namespace Edgar.Logic
{
    public class CharacterPersistenceController
    {
        XmlSerializer _characterSerializer;

        public CharacterPersistenceController()
        {
            _characterSerializer = new XmlSerializer(typeof(Edgar.Entities.Characteristics.Character));
        }

        public void Save(Character character, string filename)
        {
            using (TextWriter tw = new StreamWriter(filename))
            {
                _characterSerializer.Serialize(tw, character);
            }
        }

        public Character Load(string filename)
        {
            using (TextReader tr = new StreamReader(filename))
            {
                return (Character)_characterSerializer.Deserialize(tr);
            }
        }

        public string GetCharactersDirectory()
        {
            var mainDir = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory;
            if (mainDir.Exists)
            {
                var charDir = mainDir.CreateSubdirectory("characters");
                if (charDir.Exists)
                {
                    return charDir.FullName;
                }
            }
            return null;
        }
    }
}
